package creative.fire.interview.oracle;

class EqualsHashCodeParent {
	protected final int x;
	protected final int y;

	public EqualsHashCodeParent(int x, int y) {
		this.x = x;
		this.y = y;
	}
}

public final class EqualsHashCode extends EqualsHashCodeParent {
	public EqualsHashCode(int x, int y) {
		super(x, y);
	}

	//當equals相等時，hashCode必須相等
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (this.getClass() == obj.getClass()) {
			EqualsHashCode other = (EqualsHashCode) obj;
			return this.x == other.x && this.y == other.y;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return x ^ y;
	}

	@Override
	public String toString() {
		return "x=" + x + ",y=" + y;
	}

	public static void main(String[] args) {
		EqualsHashCode thiz1 = new EqualsHashCode(1, 2);
		EqualsHashCode thiz2 = new EqualsHashCode(2, 1);
		EqualsHashCodeParent thiz3 = new EqualsHashCode(2, 1);
		EqualsHashCodeParent thiz4 = new EqualsHashCodeParent(2, 1);
		System.out.println("thiz1 eq thiz2 ?" + thiz1.equals(thiz2));
		System.out.println("thiz3 eq thiz2 ?" + thiz3.equals(thiz2));
		System.out.println("thiz4 eq thiz2 ?" + thiz4.equals(thiz2));
	}
}
